package com.example.androidproject.online.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidproject.databinding.FragmentChatSentMessageBinding;
import com.example.androidproject.databinding.FragmentItemReceivedMessageBinding;
import com.example.androidproject.databinding.FragmentReceivedFileBinding;
import com.example.androidproject.databinding.FragmentSentFileBinding;
import com.example.androidproject.online.pojo.ChatInfo;

import java.util.ArrayList;

public class OnlineChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_MY_MESSAGE = 0;
    private static final int TYPE_OTHER_MESSAGE = 1;
    private static final int TYPE_MY_FILE = 2;
    private static final int TYPE_OTHER_FILE = 3;
    private ArrayList<ChatInfo> list;

    public OnlineChatAdapter(ArrayList<ChatInfo> list) {
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        ChatInfo chatInfo = list.get(position);
        if (chatInfo.getIdentity() == 0) {
            return TYPE_MY_MESSAGE;
        } else if (chatInfo.getIdentity() == 1) {
            return TYPE_OTHER_MESSAGE;
        } else if (chatInfo.getIdentity() == 2) {
            return TYPE_MY_FILE;
        } else {
            return TYPE_OTHER_FILE;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_MY_MESSAGE) {
            FragmentChatSentMessageBinding binding = FragmentChatSentMessageBinding.inflate(inflater, parent, false);
            return new MyViewHolder(binding);
        } else if (viewType == TYPE_OTHER_MESSAGE) {
            FragmentItemReceivedMessageBinding binding = FragmentItemReceivedMessageBinding.inflate(inflater, parent, false);
            return new OtherViewHolder(binding);
        } else if (viewType == TYPE_MY_FILE) {
            FragmentSentFileBinding binding = FragmentSentFileBinding.inflate(inflater, parent, false);
            return new MyFileViewHolder(binding);
        } else {
            FragmentReceivedFileBinding binding = FragmentReceivedFileBinding.inflate(inflater, parent, false);
            return new OtherFileViewHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatInfo chatInfo = list.get(position);

        if (holder instanceof MyViewHolder) {
            ((MyViewHolder) holder).bind(chatInfo);
        } else if (holder instanceof OtherViewHolder) {
            ((OtherViewHolder) holder).bind(chatInfo);
        } else if (holder instanceof MyFileViewHolder) {
            ((MyFileViewHolder) holder).bind(chatInfo);
        } else if (holder instanceof OtherFileViewHolder) {
            ((OtherFileViewHolder) holder).bind(chatInfo);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void update(ArrayList<ChatInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private FragmentChatSentMessageBinding binding;

        public MyViewHolder(@NonNull FragmentChatSentMessageBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(ChatInfo chatInfo) {
            binding.textMessage.setText(chatInfo.getMessage());
        }
    }

    class OtherViewHolder extends RecyclerView.ViewHolder {
        private FragmentItemReceivedMessageBinding binding;

        public OtherViewHolder(@NonNull FragmentItemReceivedMessageBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(ChatInfo chatInfo) {
            binding.textMessage.setText(chatInfo.getMessage());
        }
    }

    class MyFileViewHolder extends RecyclerView.ViewHolder {
        private FragmentSentFileBinding binding;

        public MyFileViewHolder(@NonNull FragmentSentFileBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(ChatInfo chatInfo) {
            binding.textFileName.setText(chatInfo.getFileName());
        }
    }

    class OtherFileViewHolder extends RecyclerView.ViewHolder {
        private FragmentReceivedFileBinding binding;

        public OtherFileViewHolder(@NonNull FragmentReceivedFileBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(ChatInfo chatInfo) {
            binding.textFileName.setText(chatInfo.getFileName());
        }
    }
}
